package com.obs.ttltest;


import com.alibaba.ttl.TransmittableThreadLocal;
import com.alibaba.ttl.TtlRunnable;
import com.alibaba.ttl.threadpool.TtlExecutors;

import java.util.Arrays;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Example {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //方式一：TtlRunnable封装：
        ExecutorService executorService = Executors.newCachedThreadPool();
        TransmittableThreadLocal<String> threadLocal = new TransmittableThreadLocal<>();
        threadLocal.set("父亲");
        TtlRunnable runnable = TtlRunnable.get(() -> System.out.println(threadLocal.get()));
        Future<?> submit = executorService.submit(runnable);
        System.out.println(submit.get());
        executorService.shutdown();

        //方式二：ExecutorService封装：
        ExecutorService executorService1 = Executors.newCachedThreadPool();
        ExecutorService ttlExecutorService = TtlExecutors.getTtlExecutorService(executorService1);
        System.out.println(ttlExecutorService.submit(() -> "123").get());
        ttlExecutorService.shutdown();

        //方式三：使用java agent，无代码入侵
        ExecutorService executorService2 = Executors.newCachedThreadPool();
        TransmittableThreadLocal<String> context = new TransmittableThreadLocal<>();
        context.set("父亲");
        String s = executorService2.submit(context::get).get();
        System.out.println(s);
        String s1 = executorService2.submit(context::get).get();
        System.out.println(s1);
        String s2 = executorService2.submit(context::get).get();
        System.out.println(s2);
        context.remove();
        String s3 = executorService2.submit(context::get).get();
        System.out.println("s3:"+ s3);
        executorService2.shutdown();

        System.out.println(Arrays.toString(CtCertInfoType.values()));

    }
}
